package com.CGFrame.struts.action.categorySet;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.CGFrame.struts.POJOs.Catemodel;
import com.CGFrame.struts.POJOs.Company;
import com.CGFrame.struts.bean.category.CateModelBean;
import com.CGFrame.struts.bean.log.LogHistoryBean;

public class CreateModelAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String name=request.getParameter("modelname");
		String type=request.getParameter("modeltype");
		String desc=request.getParameter("modeldesc");
		
		CateModelBean cmb=new CateModelBean();
		
		if(name.equals("")){
			request.setAttribute("notnull","name is null!");
			return mapping.findForward("showmodel");
		}
		
		
		List models=cmb.getModels();
		for(Iterator iter=models.iterator();iter.hasNext();){
			Catemodel model=(Catemodel)iter.next();
			if(model.getCateModelName().equals(name) && model.getCateModelType().equals(type)){
				request.setAttribute("dup", "name is duplicated!");
				return mapping.findForward("showmodel");
			}
		}
		
		HttpSession session = request.getSession();
		String companyId = (String)session.getAttribute("CompanyID");
		int resultId = cmb.createModel(companyId, name, type,desc);
		
		LogHistoryBean logBean = new LogHistoryBean();
		String regUserId = (String)request.getSession().getAttribute("RegUserID");
		logBean.saveByReguserId(regUserId, LogHistoryBean.ADD,name, "Catemodel", resultId );
		
		return mapping.findForward("showmodel");
	}
	
}
